'use strict';

const BaseController = require('./BaseController');

class ProjectController extends BaseController {
    async list() {
        const { ctx } = this;
        const projectList = await ctx.service.project.findAll();
        if (projectList) {
            ctx.success(projectList);
        } else {
            ctx.msg('查询不到项目列表');
        }
    }

    async getById() {
        const { ctx } = this;
        const id = ctx.request.query.id;
        if (id) {
            const project = await ctx.service.project.findOne(id);
            ctx.success(project);
        }
    }

    async create() {
        const { ctx } = this;
        const { name, score } = ctx.request.body;
        if (name) {
            const isSuccess = await ctx.service.project.insert({ name, score });
            if (isSuccess) {
                ctx.success(isSuccess);
            } else {
                ctx.error(-1, '项目添加失败');
            }
        }
    }

    async update() {
        const { ctx } = this;
        const { id, name, score } = ctx.request.body;
        if (id && name) {
            const isSuccess = await ctx.service.project.update({ id, name, score });
            if (isSuccess) {
                ctx.success(isSuccess);
            } else {
                ctx.error(-1, '项目更新失败');
            }
        }
    }

    async delete() {
        const { ctx } = this;
        const id = ctx.request.query.id;
        if (id) {
            const isSuccess = await ctx.service.project.delete(id);
            if (isSuccess) {
                ctx.success(isSuccess);
            } else {
                ctx.error(-1, '项目删除失败');
            }
        }
    }
}

module.exports = ProjectController;
